<?php
namespace Admin\Controller;
use Think\Controller;
class GoodsController extends BaseController {
    public function index(){
    	$this->verify_login();
        $Goods = M('Goods');
        $courseList = $Goods
            ->Field('tianjiao_goodstype.typename,tianjiao_goods.*')
            ->join('tianjiao_goodstype on tianjiao_goods.typeid=tianjiao_goodstype.id','LEFT')->select();
        $this->assign('courseList',$courseList);
        $this->display();
    }
    public function add(){
        $this->verify_login();
        $goodstype = M('goodstype');
        $goodstypeList=$goodstype->select();
        $this->assign('goodstypeList',$goodstypeList);
        $this->display();
    }
    public function addGoods(){
        $this->verify_login();
        $data['name']=I('post.name');
        $data['price']=I('post.price');
        $data['isnew']=I('post.isnew');
        $data['ishot']=I('post.ishot');
        $data['typeid']=I('post.typeid');
        $data['ispromotion']=I('post.ispromotion');
        $data['introduce']=I('post.introduce');
        $Goods=M('Goods');
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     2097152 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =      './Uploads/'; // 设置附件上传根目录
        $upload->savePath  =      'Goods/';
        $upload->autoSub = false;
        // 上传单个文件 
        $info   =   $upload->uploadOne($_FILES['mainimg']);
        if(!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        }else{// 上传成功 获取上传文件信息
            $data['mainimg']='Uploads/'.$info['savepath'].$info['savename'];
            $res=$Goods->add($data);
            if($res){
                $this->success("添加成功",U('Goods/index'));
            }else{
                $this->error("添加失败");
            }
        }
    }
    public function editCourse(){
        $this->verify_login();        
        $id=I('post.id');
        $data['name']=I('post.name');
        $data['price']=I('post.price');
        $data['isnew']=I('post.isnew');
        $data['typeid']=I('post.typeid');
        $data['ishot']=I('post.ishot');
        $data['ispromotion']=I('post.ispromotion');
        $data['introduce']=I('post.introduce');
        $Goods=M('Goods');
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     2097152 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =      './Uploads/'; // 设置附件上传根目录
        $upload->savePath  =      'Goods/';
        $upload->autoSub = false;
        // 上传单个文件 
        $info   =   $upload->uploadOne($_FILES['mainimg']);
        if(!$info) {// 上传错误提示错误信息
            // $this->error($upload->getError());
        }else{// 上传成功 获取上传文件信息
            $data['mainimg']='Uploads/'.$info['savepath'].$info['savename'];
        }
        $res=$Goods->where("id=".$id)->save($data);
        if($res){
            $this->success("修改成功",U('Goods/index'));
        }else{
            $this->error("修改失败");
        }
    }
    public function edit(){
        $this->verify_login();
        $id=I('get.id');
        $Goods=M('Goods');
        $res=$Goods->where('id='.$id)->find();
        $this->assign('res',$res);
        $goodstype = M('goodstype');
        $goodstypeList=$goodstype->select();
        $this->assign('goodstypeList',$goodstypeList);
        $this->display();
    }
    public function del(){
        $this->verify_login();
    	$id=I('get.id');
    	$Goods=M('Goods');
    	$res=$Goods->where('id='.$id)->delete();
    	if($res){
        	$this->success("删除成功",U('Goods/index'));
        }else{
        	$this->error("删除失败");
        }
    }
    public function goodsType(){
        $this->verify_login();
        $goodstype = M('goodstype');
        $goodstypeList = $goodstype->select();
        $this->assign('goodstypeList',$goodstypeList);
        $this->display();
    }
    public function goodstypeAdd(){
        $this->verify_login();
        $this->display();
    }
    public function addGoodstype(){
        $this->verify_login();
        $data['typename']=I('post.name');

        $goodstype=M('goodstype');
        $res=$goodstype->add($data);
        if($res){
            $this->success("添加成功",U('Goods/goodsType'));
        }else{
            $this->error("添加失败");
        }
        
    }

    public function delGoodstype(){
        $this->verify_login();
        $id=I('get.id');
        $goodstype=M('goodstype');
        $res=$goodstype->where('id='.$id)->delete();
        if($res){
            $this->success("删除成功",U('Goods/goodsType'));
        }else{
            $this->error("删除失败");
        }
    }
    public function editGoodstype(){
        $this->verify_login();
        $id=I('get.id');
        $goodstype=M('goodstype');
        $res=$goodstype->where('id='.$id)->find();
        $this->assign('res',$res);
        $this->display();
    }
    public function saveGoodstype(){
        $this->verify_login();        
        $id=I('post.id');
        $data['typename']=I('post.name');
        $goodstype=M('goodstype');
        $res=$goodstype->where("id=".$id)->save($data);
        if($res){
            $this->success("修改成功",U('Goods/goodsType'));
        }else{
            $this->error("修改失败");
        }
    }
}